package com.book.util;

import org.apdplat.word.WordSegmenter;
import org.apdplat.word.segmentation.SegmentationAlgorithm;
import org.apdplat.word.segmentation.Word;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author LiJinCheng
 * @Date 2020/11/17 1:30 下午
 * @Version 1.0
 * @package com.book.util
 **/
public class WordUtil {
    public static Map<String, String> segMore(String text) {
        Map<String, String> map = new HashMap<>();
        for(SegmentationAlgorithm segmentationAlgorithm : SegmentationAlgorithm.values()){
            map.put(segmentationAlgorithm.getDes(), seg(text, segmentationAlgorithm));
        }
        return map;
    }
    private static String seg(String text, SegmentationAlgorithm segmentationAlgorithm) {
        StringBuilder result = new StringBuilder();
        for(Word word : WordSegmenter.segWithStopWords(text, segmentationAlgorithm)){
            result.append(word.getText()).append(" ");
        }
        return result.toString();
    }

    public static String segWord(String text){
        Map<String, String> cx = segMore(text);
        for (String s:cx.keySet()
        ) {
            if (s.equals("最少词数算法")){
                return cx.get(s);
            }
        }
        return null;
    }
}
